package edu.pw.p2pclient.activities;

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import edu.pw.p2pclient.R;
import edu.pw.p2pclient.fragment.AvailableFragment;
import edu.pw.p2pclient.fragment.DownloadQueueFragment;
import edu.pw.p2pclient.fragment.PushDownloadedFragment;
import edu.pw.p2pclient.fragment.PushUploadFragment;
import edu.pw.p2pclient.fragment.SharedFragment;
import edu.pw.p2pclient.networking.CommunicationService;
import edu.pw.p2pclient.utils.ApplicationContext;

public class MainActivity extends ActionBarActivity {

	public static final int PUSH_DOWNLOADED = 0;
	public static final int DOWNLOAD_QUEUE = 1;
	public static final int SHARED = 2;
	public static final int AVAILABLE = 3;
	public static final int PUSH_UPLOAD = 4;

	private CollectionPagerAdapter mCollectionPagerAdapter;
	private ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		ApplicationContext.init(getApplicationContext());
		setContentView(R.layout.activity_main);

		// ViewPager and its adapters use support library
		// fragments, so use getSupportFragmentManager.
		mCollectionPagerAdapter = new CollectionPagerAdapter(
				getSupportFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mCollectionPagerAdapter);

		if (PreferenceManager.getDefaultSharedPreferences(
				getApplicationContext()).getBoolean(
				SettingsActivity.SHARING_PREF, false)
				&& !CommunicationService.isRunning())
			startService(new Intent(getApplicationContext(),
					CommunicationService.class));

		Thread.currentThread().setName("UI Thread");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.shareItem: {
			Intent addSharedIntent = new Intent(this, AddToSharedActivity.class);
			startActivity(addSharedIntent);
			return true;
		}
		case R.id.resourcesSearchItem:
			Intent searchIntent = new Intent(this,
					ResourcesSearchActivity.class);
			startActivity(searchIntent);
			return true;
		case R.id.settingsItem: {
			Intent settingsIntent = new Intent(this, SettingsActivity.class);
			startActivity(settingsIntent);
			return true;
		}
		}
		return super.onOptionsItemSelected(item);
	}

	// Since this is an object collection, use a FragmentStatePagerAdapter,
	// and NOT a FragmentPagerAdapter.
	public class CollectionPagerAdapter extends FragmentStatePagerAdapter {

		private Fragment[] mFragments;

		public CollectionPagerAdapter(FragmentManager fm) {

			super(fm);
			mFragments = new Fragment[5];
		}

		@Override
		public Fragment getItem(int i) {

			if (mFragments[i] != null)
				return mFragments[i];

			switch (i) {
			case PUSH_DOWNLOADED:
				if (mFragments[i] == null)
					mFragments[i] = new PushDownloadedFragment();
				break;
			case DOWNLOAD_QUEUE:
				if (mFragments[i] == null)
					mFragments[i] = new DownloadQueueFragment();
				break;
			case SHARED:
				if (mFragments[i] == null)
					mFragments[i] = new SharedFragment();
				break;
			case AVAILABLE:
				if (mFragments[i] == null)
					mFragments[i] = new AvailableFragment();
				break;
			case PUSH_UPLOAD:
				if (mFragments[i] == null)
					mFragments[i] = new PushUploadFragment();
				break;
			default:
				return null;
			}
			return mFragments[i];
		}

		@Override
		public int getCount() {
			return 5;
		}

		@Override
		public CharSequence getPageTitle(int position) {

			switch (position) {
			case PUSH_DOWNLOADED:
				return PushDownloadedFragment.TITLE;
			case DOWNLOAD_QUEUE:
				return DownloadQueueFragment.TITLE;
			case SHARED:
				return SharedFragment.TITLE;
			case AVAILABLE:
				return AvailableFragment.TITLE;
			case PUSH_UPLOAD:
				return PushUploadFragment.TITLE;
			default:
				return "";
			}
		}
	}

	@Override
	public void onBackPressed() {

		if (mViewPager.getCurrentItem() == SHARED
				&& ((SharedFragment) mCollectionPagerAdapter.getItem(SHARED))
						.onBackPressed())
			return;
		super.onBackPressed();
	}

}
